﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Daycare.Models;
using System.Data.SqlClient;

namespace Daycare.DAL
{
    public class ChildRepository
    {
        public int InsertChild(string firstName, string middleName, string lastName, string nickName, DateTime dateOfBirth, int familyId, int imageId, bool isActive = true)
        {
            using (SqlConnection con = new SqlConnection(Configuration.ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("daycare.InsertChild", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.AddWithValue("@firstName", firstName);
                    cmd.Parameters.AddWithValue("@middleName", middleName);
                    cmd.Parameters.AddWithValue("@lastName", lastName);
                    cmd.Parameters.AddWithValue("@nickName", nickName);
                    cmd.Parameters.AddWithValue("@dateOfBirth", dateOfBirth);
                    cmd.Parameters.AddWithValue("@familyId", familyId);
                    cmd.Parameters.AddWithValue("@imageId", imageId);
                    cmd.Parameters.AddWithValue("@isActive", isActive);

                    con.Open();
                    familyId = int.Parse(cmd.ExecuteScalar().ToString());
                    con.Close();


                    return familyId;
                }
            }
        }

        public void UpdateChild(int childId, string firstName, string middleName, string lastName, string nickName, DateTime dateOfBirth, int imageId, bool isActive)
        {
            using (SqlConnection con = new SqlConnection(Configuration.ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("daycare.UpdateChild", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@childId", childId);
                    cmd.Parameters.AddWithValue("@firstName", firstName);
                    cmd.Parameters.AddWithValue("@middleName", middleName);
                    cmd.Parameters.AddWithValue("@lastName", lastName);
                    cmd.Parameters.AddWithValue("@nickName", nickName);
                    cmd.Parameters.AddWithValue("@dateOfBirth", dateOfBirth);
                    cmd.Parameters.AddWithValue("@imageId", imageId);
                    cmd.Parameters.AddWithValue("@isActive", isActive);

                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
    }
}